<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>机房管理-机房列表</title>
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/layuimini.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/themes/default.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        .div-content{
            background-color: white;
            border-radius: 5px;
            padding: 14px;
        }
        .table-search-fieldset{
            margin: 0;
            border: 1px solid #e6e6e6;
            padding: 10px 20px 5px 20px;
            color: #6b6b6b;
        }
        .zhuyi{
            color: red;
            width: 720px;
        }
    </style>
</head>
<body>
<div class="div-content">
    <!--搜索条件查询-->
    <form class="layui-form layui-form-pane" action="" id="form-search">
        <fieldset class="table-search-fieldset">
            <legend>搜索条件</legend>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 90px">机房地址</label>
                    <div class="layui-input-inline" style="width: 150px;">
                        <input type="text" name="name" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 60px">校区</label>
                    <div class="layui-input-inline" style="width: 120px;">
                        <select name="campus">
                            <option value="">请选择</option>
                            <option value="1">花津校区</option>
                            <option value="2">赭山校区</option>
                            <option value="3">天门山校区</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px">空闲时间</label>
                    <div class="layui-input-inline" style="width: 60px;">
                        <input type="checkbox" name="aempty" title="上午" lay-skin="primary" value="1"  class="layui-input">
                    </div>
                    <div class="layui-input-inline" style="width: 60px;">
                        <input type="checkbox" name="pempty" title="下午" lay-skin="primary" value="1" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 60px">容量</label>
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="text" name="lownumber" placeholder="最低人数" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="text" name="highnumber" placeholder="最高人数" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <button id="btn-search" lay-submit lay-filter="btn-search-filter" type="button" class="layui-btn layui-btn-radius layui-btn-normal">
                        <i class="layui-icon layui-icon-search"></i>
                        搜索
                    </button>
                    <button id="btn-reset" type="reset" class="layui-btn layui-btn-radius layui-btn-primary">
                        <i class="layui-icon layui-icon-fonts-clear"></i>
                        清空条件
                    </button>
                </div>
            </div>
        </fieldset>
    </form>

    <fieldset style="width: 730px">
        <legend style="color: red">注意</legend>
        <p class="zhuyi">
            请尽可能在考生安排之前配置好机房信息，否则删除机房操作会将原本安排在该机房的考生全部恢复到未安排状态。
        </p>
    </fieldset>

    <!--表格显示-->
    <table id="table-booklist" lay-filter="table-booklist-filter"></table>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/jquery-3.6.0.min.js}" charset="utf-8"></script>

<script type="text/html" id="table-book-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</button>
    </div>
</script>

<script type="text/html" id="col-book-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script th:inline="javascript">

    //刷新页面
    function refreshTable(){
        $("#btn-reset").click();
        $("#btn-search").click();
    }

    //点击清空条件时刷新页面
    $("#btn-reset").click(function () {
        window.location.reload()
    })

    layui.use(['laydate','table','form'],function () {
        var laydate=layui.laydate;
        var table=layui.table;
        var form=layui.form;

        //表格头部工具栏监听（新增和删除）
        table.on('toolbar(table-booklist-filter)', function(obj){
            if(obj.event=="add"){  //表头的新增
                layer.open({
                    type: 2,
                    title:"新增机房",
                    area:['40%','90%'],
                    content: '/room/add'
                });
            }else if(obj.event=="delete"){  //表头的批量删除
                var checkStatus = table.checkStatus('table-booklist'); //idTest 即为基础参数 id 对应的值
                console.log(checkStatus.data) //获取选中行的数据
                //没选中数据
                if(checkStatus.data.length==0){
                    layer.alert("您没有选中任何数据！",{icon:2,anim:6,title:"警告"});
                    return false;
                }
                //选中id
                var idArr=new Array();
                $(checkStatus.data).each(function () {
                    idArr.push(this.id);
                });
                console.log(idArr.join(","));

                layer.confirm('您确认删除吗?', {icon: 3, title:'删除确认'}, function(index){
                    // 异步请求，删除数据
                    $.ajax({
                        url:"/room/delete/"+idArr.join(","),
                        success:function (response) {
                            if(response.state==200){
                                layer.msg(response.message,{
                                    icon: 1,
                                    time: 1000
                                },function () {
                                    $("#btn-search").click();
                                });
                            }else {
                                layer.alert("删除失败！",{icon:2,anim:6});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //执行一个laydate实例
        laydate.render({
            elem:".date"
        });

        //加载页面时机房列表表格渲染
        table.render({
            elem: '#table-booklist'
            ,url: '/showRoom' //数据接口
            ,page: true //开启分页
            ,toolbar:"#table-book-toolbar"
            ,limits:[6,8,10,20]
            ,cols: [
                [ //表头
                {type:"checkbox",width: 70, rowspan:2},
                {field: 'roomAddr', title: '机房地址',width:250,align:'center', rowspan:2}
                ,{field: 'roomCap', title: '人数', width:140,align:'center', rowspan:2}
                ,{field: 'campus', title: '校区', width:160,align:'center', rowspan:2,
                    templet:function (d) {
                        if(d.campus=='1') return '花津校区';
                        if(d.campus=='2') return '赭山校区';
                        if(d.campus=='3') return '天门山校区';
                    }
                }
                ,{title:'空闲时间', colspan:2, align:'center', width:180}
                ,{title: '操作', width: 170,align:'center',toolbar: '#col-book-bar', rowspan:2}
                ],
                [
                    {field: 'aempty', title: '上午', width: 90, align:'center',
                        templet:function (d) {
                            console.log(d.aempty)
                            return d.aempty==1?'<input style="border: 1px solid black" type="checkbox" lay-skin="primary" checked disabled class="layui-input">':'<input style="border: 1px solid black" type="checkbox" lay-skin="primary" disabled class="layui-input">'
                        }
                    },
                    {field: 'pempty', title: '下午', width: 90,align:"center",
                        templet:function (d) {
                            console.log(d.pempty)
                            return d.pempty==1?'<input type="checkbox" lay-skin="primary" checked disabled class="layui-input">':'<input type="checkbox" lay-skin="primary" disabled class="layui-input">'
                        }
                    }
                ]
            ]
        });

        //监听表单提交（根据搜索条件查询机房）
        form.on('submit(btn-search-filter)', function(data){
            // console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            // console.log(data.field.pempty)
            let substr="";
            let arr={"name":data.field.name, "campus":data.field.campus,"aempty":data.field.aempty,"pempty":data.field.pempty,
                "lownumber":data.field.lownumber, "highnumber":data.field.highnumber};
            for(let i in arr){
                if(arr[i]===""||arr[i]===undefined){delete arr[i]}
            }
            // console.log(arr)
            for(let j in arr){substr+="&"+j+"="+arr[j];}
            // console.log(substr)
            substr=substr.substring(1);
            // console.log(substr)

            table.render({
                elem: '#table-booklist'
                ,url: '/showRoom?'+substr //数据接口
                ,page: true //开启分页
                ,toolbar:"#table-book-toolbar"
                ,limits:[6,8,10,20]
                ,cols: [
                    [ //表头
                        {type:"checkbox",width: 70, rowspan:2},
                        {field: 'roomAddr', title: '机房地址',width:250,align:'center', rowspan:2}
                        ,{field: 'roomCap', title: '人数', width:140,align:'center', rowspan:2}
                        ,{field: 'campus', title: '校区', width:160,align:'center', rowspan:2,
                        templet:function (d) {
                            if(d.campus=='1') return '花津校区';
                            if(d.campus=='2') return '赭山校区';
                            if(d.campus=='3') return '天门山校区';
                        }
                    }
                        ,{title:'空闲时间', colspan:2, align:'center', width:180}
                        ,{title: '操作', width: 170,align:'center',toolbar: '#col-book-bar', rowspan:2}
                    ],
                    [
                        {field: 'aempty', title: '上午', width: 90, align:'center',
                            templet:function (d) {
                                console.log(d.aempty)
                                return d.aempty==1?'<input style="border: 1px solid black" type="checkbox" lay-skin="primary" checked disabled class="layui-input">':'<input style="border: 1px solid black" type="checkbox" lay-skin="primary" disabled class="layui-input">'
                            }
                        },
                        {field: 'pempty', title: '下午', width: 90,align:"center",
                            templet:function (d) {
                                console.log(d.pempty)
                                return d.pempty==1?'<input type="checkbox" lay-skin="primary" checked disabled class="layui-input">':'<input type="checkbox" lay-skin="primary" disabled class="layui-input">'
                            }
                        }
                    ]
                ]
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //操作表格的操作列
        table.on('tool(table-booklist-filter)', function(obj){
            var data = obj.data; //获得当前行数据
            console.log(data);
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent=='del'){  //操作中的删除
                layer.confirm('您确认删除该记录吗?', {icon: 3, title:'删除确认'}, function(index){
                    //异步请求，删除数据
                    $.ajax({
                        url:"/room/delete/"+data.roomId,
                        success:function (response) {
                            if(response.state==200){
                                layer.msg(response.message,{
                                    icon: 1,
                                    time: 1000
                                },function () {
                                    $("#btn-search").click();
                                });
                            }else {
                                layer.alert("删除失败！",{icon:2,anim:6});
                            }
                        }
                    });
                    layer.close(index);
                });
            }else if(layEvent=='edit'){   //每一行的编辑操作
                layer.open({
                    type: 2,
                    title:"修改机房",
                    area:['40%','90%'],
                    content: '/room/find/'+data.roomId
                });
            }
        });
    });
</script>
</body>
</html>